package com.cqcet.wenchuang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqcet.wenchuang.common.R;
import com.cqcet.wenchuang.dto.OrderDetailsDto;
import com.cqcet.wenchuang.dto.OrderDto;
import com.cqcet.wenchuang.entity.Goods;
import com.cqcet.wenchuang.entity.OrderDetails;
import com.cqcet.wenchuang.entity.Orders;
import com.cqcet.wenchuang.service.GoodsService;
import com.cqcet.wenchuang.service.OrderDetailsService;
import com.cqcet.wenchuang.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "订单详情管理接口")
@RestController
@RequestMapping("/orderDetails")
public class OrderDetailsController {

    @Autowired
    private OrderDetailsService orderDetailsService;

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private GoodsService goodsService;

    @ApiOperation(value = "修改订单详情", notes = "可选:province,city,details,phone,consignee,remark")
    @PutMapping
    public R<String> update(@RequestBody OrderDetails orderDetails) {
        orderDetailsService.updateById(orderDetails);
        return R.success("修改成功");
    }

    /**
     * 根据订单详编号查询
     *
     * @param code
     * @return
     */
    @ApiOperation("根据订单详编号查询")
    @GetMapping("/search/{code}")
    public R<OrderDetailsDto> getOne(@PathVariable String code) {
        LambdaQueryWrapper<OrderDetails> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(code), OrderDetails::getCode, code);
        //订单详情
        OrderDetails orderDetails = orderDetailsService.getOne(queryWrapper);
        //创建dto对象传送信息
        OrderDetailsDto orderDetailsDto = new OrderDetailsDto();
        //获取商品信息
        Goods goods = goodsService.getById(orderDetails.getGoodsId());
        BeanUtils.copyProperties(goods, orderDetailsDto);
        //将详情信息加入到dto对象中
        BeanUtils.copyProperties(orderDetails, orderDetailsDto, "createTime","updateTime");
        //获取订单信息
        Orders orders = ordersService.getById(orderDetails.getOrderId());
        orderDetailsDto.setOrders(orders);

        return R.success(orderDetailsDto);
    }

    /**
     * 商家查看所有订单
     *
     * @param page
     * @param pageSize
     * @param code
     * @param adminId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页多少", required = true),
            @ApiImplicitParam(name = "code", value = "订单编号"),
    })
    @ApiOperation("商家查看所有订单")
    @GetMapping("/admin/{adminId}")
    public R<Page<OrderDetailsDto>> getList(Integer page, Integer pageSize, String code, @PathVariable Long adminId) {
        Page<OrderDetails> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<OrderDetails> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtils.isNotNull(adminId), OrderDetails::getAdminId, adminId);
        queryWrapper.eq(StringUtils.isNotBlank(code), OrderDetails::getCode, code);
        //按照创建时间降序排列
        queryWrapper.orderByDesc(OrderDetails::getCreateTime);

        Page<OrderDetails> page1 = orderDetailsService.page(pageInfo, queryWrapper);

        List<OrderDetailsDto> collect = page1.getRecords().stream().map((orderDetail) -> {
            //商品信息
            Goods goods = goodsService.getById(orderDetail.getGoodsId());
            OrderDetailsDto orderDetailsDto = new OrderDetailsDto();
            //将订单详情的信息拷贝至数据传输对象
            BeanUtils.copyProperties(orderDetail, orderDetailsDto);
            BeanUtils.copyProperties(goods, orderDetailsDto);
            //订单信息
            Orders orders = ordersService.getById(orderDetail.getOrderId());
            orderDetailsDto.setOrders(orders);
            return orderDetailsDto;
        }).collect(Collectors.toList());

        Page<OrderDetailsDto> orderDetailsDtoPage = new Page<>();
        BeanUtils.copyProperties(page1, orderDetailsDtoPage, "records");
        orderDetailsDtoPage.setRecords(collect);

        return R.success(orderDetailsDtoPage);
    }

    @ApiOperation("用户查询所有订单")
    @GetMapping("/{userId}")
    public R<List<OrderDto>> list(@PathVariable Long userId) {
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtils.isNotEmpty(userId), Orders::getUserId, userId);
        //全部订单
        List<Orders> ordersList = ordersService.list(queryWrapper);

        List<OrderDto> orderDtos = ordersList.stream().map(orders -> {
            OrderDto orderDto = new OrderDto();
            BeanUtils.copyProperties(orders, orderDto);

            LambdaQueryWrapper<OrderDetails> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(OrderDetails::getOrderId, orders.getId());
            //订单详细列表
            List<OrderDetails> orderDetails = orderDetailsService.list(queryWrapper1);

            List<OrderDetailsDto> orderDetailsDtoList = orderDetails.stream().map(orderDetail -> {
                OrderDetailsDto orderDetailsDto = new OrderDetailsDto();
                Goods goodsInfo = goodsService.getById(orderDetail.getGoodsId());
                BeanUtils.copyProperties(goodsInfo, orderDetailsDto, "id");
                orderDetailsDto.setGoodsId(goodsInfo.getId());
                BeanUtils.copyProperties(orderDetail, orderDetailsDto);
                return orderDetailsDto;
            }).collect(Collectors.toList());
            orderDto.setOrderDetailsDtoList(orderDetailsDtoList);
            return orderDto;
        }).collect(Collectors.toList());

        return R.success(orderDtos);
    }

}